<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :title="title"
    width="700px">
    <dynForm
      ref="dataForm"
      :info-form="searchForm"
      :search-tags="searchTags"
      :select-list="selectList"
      :rules="rules"
      :is-inline="false"
    />
    <span slot="footer">
      <el-button icon="el-icon-circle-close" @click="handleClose">{{
        $t("cancel")
      }}</el-button>
      <el-button
        type="primary"
        icon="el-icon-receiving"
        @click="handleSave"
      >{{ $t('save') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { doAdd, doUpdate, getInfo, getAllTypes, getAllMaterialCode } from '@/api/configSetting/materialMachineBinding'
import { getEqpTypeList } from '@/api/production/layoutDesign'

export default {
  name: 'MachineBinding',
  data() {
    return {
      dialogVisible: false,
      title: '',
      searchForm: {
        bindingType: 1,
        materialTypeID: undefined,
        materialCodeID: undefined
      },
      selectList: {},
      searchTags:
        [
          { label: this.$t('equipmentType'), prop: 'equipmentTypeID', tag: 'DynSelect', style: { width: '100%' }},
          { label: '绑定类别', prop: 'bindingType', tag: 'DynRadio', render: () => {
            return (
              <el-radio-group vModel={this.searchForm.bindingType} on-change={this.changeRadio}>
                <el-radio label={1}>按材料类别</el-radio>
                <el-radio label={2}>按材料编号</el-radio>
              </el-radio-group>
            )
          } },
          { label: this.$t('materialType'), prop: 'materialTypeID', tag: 'DynSelect', vif: () => {
            return this.searchForm.bindingType === 1
          }, style: { width: '100%' }},
          { label: this.$t('materialCode'), prop: 'materialCodeID', tag: 'DynSelect', vif: () => {
            return this.searchForm.bindingType === 2
          }, style: { width: '100%' }},
          { label: this.$t('description'), prop: 'description', tag: 'DynTextarea', row: 3, show_word_limit: true, maxlength: 200 }
        ],
      rules: {
        equipmentTypeID: [{ required: true, message: `${this.$t('pleaseSelect')}${this.$t('equipmentType')}`, trigger: 'change' }],
        bindingType: [{ required: true, message: this.$t('entertheproductmodel'), trigger: 'change' }],
        materialTypeID: [{ required: true, message: `${this.$t('pleaseSelect')}${this.$t('materialType')}`, trigger: 'change' }],
        materialCodeID: [{ required: true, message: `${this.$t('pleaseSelect')}${this.$t('materialCode')}`, trigger: 'change' }]
        // materialTypeID: [{ validator: materialTypePass, trigger: 'change' }],
        // materialCodeID: [{ validator: materialCodePass, trigger: 'change' }]
      }
    }
  },
  methods: {
    handleOpen(row) {
      if (row && row.id) {
        this.title = this.$t('modify')
        getInfo({ id: row.id }).then(res => {
          this.searchForm = Object.assign({}, this.searchForm, res.data)
        })
      } else {
        this.title = this.$t('add')
      }
      this.getEquipmentTypes()
      this.getMaterialTypes()
      this.getMaterialCode()
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].$refs['dynForm'].clearValidate()
      })
    },
    handleClose() {
      Object.assign(this.$data, this.$options.data.call(this))
    },
    handleUpdate() {
      this.$emit('initList')
    },
    handleSave() {
      this.$refs['dataForm'].$refs['dynForm'].validate((valid) => {
        if (valid) {
          if (this.searchForm.id) {
            // const { equipmentTypeID, materialTypeID, materialCodeID, description, id } = this.searchForm
            doUpdate(this.$clearNull(this.searchForm)).then(res => {
              this.$betterTips(res, this.handleClose, this.handleUpdate)
            })
          } else {
            // const { equipmentTypeID, materialTypeID, materialCodeID, description } = this.searchForm
            doAdd(this.$clearNull(this.searchForm)).then(res => {
              this.$betterTips(res, this.handleClose, this.handleUpdate)
            })
          }
        }
      })
    },
    changeRadio(value) {
      if (value === 1) {
        this.searchForm.materialCodeID = undefined
      } else {
        this.searchForm.materialTypeID = undefined
      }
    },
    // 获取设备类型列表
    async getEquipmentTypes() {
      const res = await getEqpTypeList()
      this.$set(this.selectList, 'equipmentTypeID', res.data)
    },
    async getMaterialTypes() {
      const res = await getAllTypes()
      this.$set(this.selectList, 'materialTypeID', res.data)
    },
    async getMaterialCode() {
      const res = await getAllMaterialCode()
      this.$set(this.selectList, 'materialCodeID', res.data)
    }
  }
}
</script>
